package com.yanqu.road.server.manger;

import com.yanqu.road.entity.mall.HideMallData;
import com.yanqu.road.entity.mall.ShieldMallMarkIdData;
import com.yanqu.road.logic.bussiness.DaoHelper;
import com.yanqu.road.logic.bussiness.servercenter.ServerBussiness;
import com.yanqu.road.pb.common.CommonProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.ClientProtocol;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HideMallMgr extends TempMgr {

    private static List<HideMallData> hideMallDataList = new ArrayList<>();

    private static Map<Integer, ShieldMallMarkIdData> shieldMarkIdMap = new HashMap<>();

    @Override
    public boolean reload() throws Exception {
        int channelId = ServerListMgr.getServerInfo(GameServer.getInstance().getServerId()).getChannelId();
        shieldMarkIdMap = DaoHelper.getServerInfoDaoImpl().getShieldMallMarkIdDataList(channelId);
        hideMallDataList = ServerBussiness.getHideMallList(channelId);
        for (GamePlayer player : GamePlayerMgr.getPlayerMap().values()) {
            syncHideMallData(player);
        }
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static void syncHideMallData(GamePlayer player) {
        CommonProto.SyncHideMallMsg.Builder tempMsg = CommonProto.SyncHideMallMsg.newBuilder();
        if (hideMallDataList != null) {
            for (HideMallData data : hideMallDataList) {
                CommonProto.HideMallMsg.Builder msg = CommonProto.HideMallMsg.newBuilder();
                msg.setMallId(data.getMallId());
                msg.setPlatformId(data.getPlatform());
                tempMsg.addMallList(msg);
            }
        }
        player.sendPacket(ClientProtocol.U_SYNC_HIDE_MALL, tempMsg);
    }

    public static Map<Integer, ShieldMallMarkIdData> getShieldMarkIdMap() {
        return shieldMarkIdMap;
    }

    public static boolean markIdIsShield(int markId){
        ShieldMallMarkIdData data = shieldMarkIdMap.get(markId);
        if(data == null){
            return false;
        }
        return data.getState() == 1;
    }
}
